;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var router = require("../hash/pageRouter");
	var validate = require("validate");
	var databind = require("databind");
	var css_sample = require("./costtypes.css");
	var pageIndex = 1, pageSize = 10, totalPageCount = 1, totalRecordCount = 0; // 分页初始化
	var html_create;
	var html_createMember = require("./costtypes-createGrade.html");
	   var html_cost_delete = require("./cost-delect.html");
	var html_delete="";
	var imgSrc;
	var costtypesId;
	var costInfoId;
	var creditLine;
	var interestRate;
	var gradeName;
	var defaultInterest;
	var remark;
	var dialogCss = require("css_dialog");
	var tools = require("tools");
	var init = function() {
		initEvent();
		router.init();
		getParamList();
		inquireCurrentOperationList();
	};
	var initEvent = function() {
		// 若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == ""
				|| base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		$("#information").unbind("click").click(function() {
			$("#popdialog").html(html_createMember);
			//tools.requireInput($("#add_costtypes_dialog"));
			dialog.showDialog($("#add_costtypes_dialog"));
			$(".sure_yes").unbind("click").click(function() {
				newad();
			});
		});
		//点击删除广告位通知
		$("#costtypes_tbody").on("click", ".costtypesDelBtn", function() {
			$("#popdialog").html(html_cost_delete);
			var $this = $(this).parents("tr.tr_style");
			var costInfoId = [];
			costInfoId.push($this.attr("costInfoId"));
			deletead(costInfoId);
		});
		
			$("#add_costtypes_dialog").on("click", ".sure_no", function() {
				dialog.hideDialog($("#add_costtypes_dialog"));
		});
	};
		// 增加
		var newad = function() {
		var promptDialog = validate.validateInput($("#add_costtypes_dialog"));
		if (promptDialog.length != 0) {
			util.alert(promptDialog);
		} else {
			var body = base.parseForm($("#add_costtypes_dialog"));
			body.costInfoNumber = $("#costInfoNumber").val();
			body.costInfoType = $("#money").val();
			body.costInfoRemark = $("#state").val();
			body.example = imgSrc;
			base.fnAjax({
				action : "createCostInfo.json", // 接口名称
				async : false, // 同步异步
				nocode : false, // 是否要登陆，true为不用登陆（其他页面不用传该参数，默认为false）
				oData : body, // 传递参数
				fnSuccess : function(data) { // 成功回调
					if (data.status == 200) {
						base.alert(1, "添加成功!");
						dialog.hideDialog($("#add_costtypes_dialog"));
						router.route("costtypess/costtypes.html");
					} else {
						base.alert(3, data.message);
					}
					dialog.hideDialog($("#add_costtypes_dialog"));
				}
			});
		}
	};
	
	
	// 权限控制
	var inquireCurrentOperationList = function() {
		base.fnAjax({
			action : 'inquireCurrentOperationList.json',
			async : false,
			fnSuccess : function(data) {
				if (data.status == 200) {
					for (var i = 0; i < data.datas.length; i++) {
						var permission = data.datas[i];
						var permissionId = permission.permissionId;
						$.each($("#costtypes .permissionLink"), function() {
							var $this = $(this);
							if ($this.attr("permissionId") == permissionId) {
								$this.removeClass("none");
							}
						});
					}
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	// 获取系统参数列表
	var getParamList = function() {
		base.fnAjax({		//inquireSystemParamList
			action : "inquireCostInfoPagelist.json",
			oData : {
				pageIndex : pageIndex,
				pageSize : pageSize,
				/*paramName : paramName,
				paramType : 0*/
			}, // 传递参数
			fnSuccess : function(data) {
				if (data.status == 200) {
					var tpl = $("#costtypes_data_tpl").html();
					var html = juicer(tpl, data);
					$("#costtypes_tbody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#costtypes_pagenation"), 
							totalRecordCount,
							data.pageIndex,
							data.pageSize,
							function(p) {
								pageIndex = p;
								getParamList();
							});
					/*inquireCurrentOperationList();*/
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	// 点击删除广告调用接口
	var deletead = function(costInfoId) {
		var delete_ad_dialog = $("#delete_costtype_dialog");
		dialog.showDialog(delete_ad_dialog, function() {});
		$(".footer-Btn").on("click", "#cost_delete", function() {
			base.fnAjax({
				action: "disableCostInfo.json", //接口名称
				oData: {
					costInfoId: costInfoId
				}, //传递参数
				fnSuccess: function(data) { //成功回调
					if (data.status == 200) {
						base.alert(1, "删除成功！");
						router.route("costtypess/costtypes.html");
					} else {
						base.alert(3, data.message);
					}
					dialog.hideDialog($("#delete_costtype_dialog"));
				}
			});
		});
	};
	return {
		init : init
	};
});